//
//  ViewController.swift
//  DropDownMenu
//
//  Created by 在成长开发 on 2024/6/24.
//  Copyright © 2024 刘爽. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 200, y: 200, width: 64, height: 36)
        btn.backgroundColor = .red
        btn.setTitle("show", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        view.addSubview(btn)
        btn.addTarget(self, action: #selector(handleClickBtn(_:)), for: .touchUpInside)

    }

    @objc func handleClickBtn(_ sender: UIButton) {
        let menu = DropDownMenu(titles: ["item 1", "item 2", "item 3", "item 4"], images: Array(repeating: "normal", count: 4))
        menu.selectedIndex = IndexPath(item: 0, section: 0)
        menu.menuItemHeight = 44
        menu.selectedImage = UIImage(named: "selected")
        menu.meunType = .rightImage
        menu.showFromPoint = CGPoint(x: CGRectGetMaxX(sender.frame), y: CGRectGetMaxY(sender.frame))
        menu.menuBackgroundView().layer.cornerRadius = 8
        menu.menuBackgroundView().backgroundColor = .brown.withAlphaComponent(0.6)
        menu.menuInset = .init(top: 10, left: 8, bottom: 10, right: 8)
        menu.autoHide = false
        menu.delegate = self
        menu.showFrom = .rightBottom
        menu.show()

    }

}

extension ViewController: DropDownMenuDelegate {

    func dropDownMenu(_ menu: DropDownMenu, didClick atIndexPath: IndexPath) {
        debugPrint("selected indexPath : \(atIndexPath)")
    }

    func dropDownMenuRegisterCustomNib(_ menu: DropDownMenu) -> String? {
        return "CustomMenuItemCell"
    }

    func dropDownMenu(_ menu: DropDownMenu, useCustomItem collectionView: UICollectionView, at indexPath: IndexPath) -> UICollectionViewCell? {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomMenuItemCell", for: indexPath) as! CustomMenuItemCell

        cell.selectedImage.isHidden = menu.selectedIndex != indexPath

        return cell
    }

}

